package cn.zyq.demo.controller.mgt.system;

import cn.zyq.demo.beans.sys.SysLogininfor;
import cn.zyq.demo.service.sys.SysLogininforService;
import cn.zyq.demo.system.annotation.Log;
import cn.zyq.demo.system.common.BaseController;
import cn.zyq.demo.system.common.PageParam;
import cn.zyq.demo.system.ry.enums.BusinessType;
import cn.zyq.demo.system.ry.security.service.SysPasswordService;
import cn.zyq.demo.system.ry.util.poi.ExcelUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 系统访问记录
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/mgt/monitor/logininfor")
public class SysLogininforController extends BaseController {
    @Autowired
    private SysLogininforService logininforService;

    @Autowired
    private SysPasswordService passwordService;

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:list')")
    @PostMapping("/list")
    public Object list(@RequestBody PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        List<SysLogininfor> list = logininforService.selectLogininforList(pageParam);
        return new PageInfo<>(list);
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody PageParam pageParam) {
        List<SysLogininfor> list = logininforService.selectLogininforList(pageParam);
        ExcelUtil<SysLogininfor> util = new ExcelUtil<SysLogininfor>(SysLogininfor.class);
        util.exportExcel(response, list, "登录日志");
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public Object remove(@PathVariable Integer[] infoIds) {
        return logininforService.deleteLogininforByIds(infoIds);
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public void clean() {
        logininforService.cleanLogininfor();
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:unlock')")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock/{userName}")
    public void unlock(@PathVariable("userName") String userName) {
        passwordService.clearLoginRecordCache(userName);
    }
}
